﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using WmWebSystem.Models;
using WmWebSystem.Service;

namespace WmWebSystem.DbHelper
{
    public static class CommentHelper
    {
        public static void AddNew(string pno, string summary)
        {
            WmDb mydb = new WmDb();
            DateTime time = DateTime.Now;
            string editor = HttpContext.Current.User.Identity.Name;
            Comment com = new Comment();
            com.ProjectNo = pno;
            com.AddTime = time;
            com.Editor = editor;
            com.Content = summary;
            try
            {
                mydb.Comments.Add(com);
                mydb.SaveChanges();
                //将记录写入日志
                string p1 = "Add new to Comments, The ProjectNo:" + pno;
                WriteLog.WriteSysLog(p1);
            }
            catch (FieldAccessException ex)
            {
                //失败，进行回滚操作  记录异常
                WriteLog.WriteSysLog(ex.ToString());
            }


        }
        public static IEnumerable<Comment> FindAll() 
        {
            WmDb mydb = new WmDb();
            var comment = from c in mydb.Comments
                          orderby c.AddTime descending
                          select c;
            return comment;
        }

        public static IEnumerable<Comment> FindByCode(string pno) 
        {
            WmDb mydb = new WmDb();
            var comment = from c in mydb.Comments
                          where c.ProjectNo == pno
                          orderby c.AddTime descending
                          select c;
            return comment;

        }



    }
}
